using GLib;
using GConf;

namespace Malem {
	protected class UnCompletedJobs : GLib.Object {
		private GConf.Client client;
	
		public UnCompletedJobs() {}
		
		construct {
			client = GConf.Client.get_default();
			try{
				client.preload("/apps/mathusalem/uncompleted-jobs", GConf.ClientPreloadType.ONELEVEL);
			} catch (GLib.Error e) {
				message("unable to preload the uncompleted-jobs folder");
			}
		}
		
		public bool addJob(string unique_command_name, string recoverCommand) {
			GLib.SList<GConf.Value> list = null;
			GConf.Value v = null;
			weak GConf.Value weak_v = null;

			GConf.Value recoverCommandValue = new GConf.Value(GConf.ValueType.STRING);
			recoverCommandValue.set_string(recoverCommand);
			try {
				weak_v = client.get("/apps/mathusalem/uncompleted-jobs/"+unique_command_name);
			} catch (GLib.Error e) {
				message(e.message);
			}
			if (weak_v==null) {
				v = new GConf.Value(GConf.ValueType.LIST);
				v.set_list_type(GConf.ValueType.STRING);
				weak_v=v;
			} else {
				weak GLib.SList weak_list = weak_v.get_list();
				weak GConf.Value wv;
				GConf.Value wv2;
				while (weak_list!=null) {
					wv = (GConf.Value) weak_list.data;
					weak_list = weak_list.next;
					wv2 = new GConf.Value(GConf.ValueType.STRING);
					wv2.set_string(wv.get_string());
					list.append(#wv2);
				}
			}
			list.append(#recoverCommandValue);
			weak_v.set_list(list);
			try {
				client.set("/apps/mathusalem/uncompleted-jobs/"+unique_command_name, weak_v);
			} catch (GLib.Error e) {
				GLib.warning("unable to save the list of uncompleted-jobs in GConf. These jobs will be never completed.");
				return false;
			}
			return true;
		}


		public bool removeJob(string unique_command_name, string recoverCommand) {
			GLib.SList<GConf.Value> list = null;
			weak GConf.Value weak_v = null;
			try {
				weak_v = client.get("/apps/mathusalem/uncompleted-jobs/"+unique_command_name);
			} catch (GLib.Error e) {
				message(e.message);
			}
			if (weak_v==null) return true;

			weak GLib.SList weak_list = weak_v.get_list();
			weak GConf.Value wv;
			GConf.Value wv2;
			if (weak_list==null) return true;
			while (weak_list!=null) {
				wv = (GConf.Value) weak_list.data;
				weak_list = weak_list.next;
				if (recoverCommand==wv.get_string()) continue;
				wv2 = new GConf.Value(GConf.ValueType.STRING);
				wv2.set_string(wv.get_string());
				list.append(#wv2);
			}

			weak_v.set_list(list);
			try {
				client.set("/apps/mathusalem/uncompleted-jobs/"+unique_command_name, weak_v);
			} catch (GLib.Error e) {
				GLib.warning("unable to save the list of uncompleted-jobs in GConf. These jobs will be never completed.");
				return false;
			}
			return true;
		}

		public bool clearAllJob(string unique_command_name) {
			GConf.Value v;
			v = new GConf.Value(GConf.ValueType.LIST);
			v.set_list_type(GConf.ValueType.STRING);
			try {
				client.set("/apps/mathusalem/uncompleted-jobs/"+unique_command_name, v);
			} catch (GLib.Error e) {
				GLib.warning("unable to save the list of uncompleted-jobs in GConf. These jobs will be never completed.");
				return false;
			}
			return true;
		}

		
		public string[] getRecoversCommandByDBusName(string unique_command_name) {
			string[] commands;
			int i=0;
			weak GConf.Value v = null;
			try {
				v = client.get("/apps/mathusalem/uncompleted-jobs/"+unique_command_name);
			} catch (GLib.Error e) {
				warning("unable to load the list of uncompleted-jobs. Maybe a GConf problem.");
				return new string[0];
			}
			if (v==null) return new string[0];
			weak GLib.SList weak_list = v.get_list();
			weak GConf.Value wv;

			commands = new string[weak_list.length()];
			while (weak_list!=null) {
				wv = (GConf.Value) weak_list.data;
				weak_list = weak_list.next;
				commands[i++] = wv.get_string();
			}
			
			return commands;
		}
	}
}
